<?php
/**
 * 评论管理
 * @author a
 *
 */
class Evaluate extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    private $substr_num = 10;   //列表显示留言数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        $this->_nursery_id = 1;     //重置园所ID为1
        
        $this->load->model('book_evaluate_model');
        $this->load->model('member_model');
        $this->load->model('course_cate_model');  
        $this->load->model('course_list_model');  
        $this->load->model('course_package_model');
        $this->load->model('teacher_model');
        $this->load->model('book_model');
        
        //分页
        $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
        $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
        $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    /**
     * 订单列表
     */
    public function Index(){
        try {
            $fild = 'book_evaluate.id,book_evaluate.member_id,book_evaluate.teacher_id,book_evaluate.star,book_evaluate.message,book_evaluate.create_date,';
            $fild.= "course_cate.title as cc_title,course_list.title as cl_title,course_package.title as cp_title";
            
            $where = " course_cate.nursery_id = '$this->_nursery_id' AND book_evaluate.status = 1 ";
            $evaluate_list = $this->book_evaluate_model->evaluate_list($where,$fild,'',$this->offse,$this->page_size);
            if(empty($evaluate_list))throw new Exception('没有评论记录');
            
            $total = $this->book_evaluate_model->evaluate_list($where,'count(1) as num'); //总数
            $pagelink = $this->pagination($total[0]['num'],$this->page_size);
            
            //获取用户名
            $where = "id IN (". filterValue($evaluate_list,'member_id',true) .")";
            $member_list = $this->member_model->findAll($where,'id,baby_name,linkman');
            $member_list = restByKey($member_list,'id');
            $evaluate_list = arrayMerge($evaluate_list,'member_id',$member_list);   //合并
            
            //获取教师
            $where = "id IN (". filterValue($evaluate_list,'teacher_id',true) .")";
            $teacher_list = $this->teacher_model->findAll($where,'id,name as teacher_name');
            $teacher_list = restByKey($teacher_list,'id');
            $evaluate_list = arrayMerge($evaluate_list,'teacher_id',$teacher_list);   //合并
            
            //echo '<pre>';print_r($evaluate_list);die();
            
        }catch (Exception $e){
            $error = $e->getMessage();
        }
        $this->t->write_view('content', 'nursery/evaluate/index', array(
            'pagelink'     =>  $pagelink,
            'evaluate_list'    =>  $evaluate_list,
            'error'     =>  empty($error) ? '' : $error,
            'substr_num'   =>   $this->substr_num,
        ), false);
        $this->t->render();
    }
    /**
     * 详情
     * @throws Exception
     */
    public function info(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('评论ID为空');
            $evaluate_list = $this->book_evaluate_model->find(array('id'=>$id));
            if(empty($evaluate_list))throw new Exception('没有评论记录');
        
            //获取用户名
            $where = "id = '{$evaluate_list['member_id']}'";
            $member_list = $this->member_model->find($where);
            
            //获取教师
            $where = "id = '{$evaluate_list['teacher_id']}'";
            $teacher_list = $this->teacher_model->find($where);
        
            //获取课包信息
            $where = "id = '{$evaluate_list['course_package_id']}'";
            $course_package_list = $this->course_package_model->find($where,'id,title,course_list_id');
            
            //获取课程列表
            $where = "id = '{$course_package_list['course_list_id']}'";
            $course_list = $this->course_list_model->find($where,'id,title,course_cate_id');
            
            //获取课程分类
            $where = "id = '{$course_list['course_cate_id']}'";
            $course_cate_list = $this->course_cate_model->find($where,'id,title');
            
            //获取上课信息
            $book_list = $this->book_model->find(array('id'=>$evaluate_list['book_id']),'book_date,book_date_start,book_date_end,message');
        
        
            //echo '<pre>';print_r($book_list);die();
        
        }catch (Exception $e){
            $error = $e->getMessage();
        }
        $this->t->write_view('content', 'nursery/evaluate/info', array(
            'teacher_list'  =>  $teacher_list,
            'book_list'    =>  $book_list,
            'evaluate_list'    =>  $evaluate_list,
            'member_list'  =>  $member_list,
            'course_cate_list' =>  $course_cate_list,
            'course_list'  =>  $course_list,
            'course_package_list'  =>  $course_package_list,
            'error'     =>  empty($error) ? '' : $error,
        ), false);
        $this->t->render();
    }
    /**
     * 修改状态
     */
    public function update(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('评论ID为空');
            $where['id'] = $id;
            $bool = $this->book_evaluate_model->update(array('status'=>0),$where);
            if(empty($bool))throw new Exception('修改失败');
            
            redirect("/nursery/evaluate");
        }catch (Exception $e){
            die($e->getMessage());
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}






























